package com.ruoyi.recipes.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.recipes.domain.Recipes;
import com.ruoyi.recipes.domain.Comments;
import com.ruoyi.recipes.domain.RecipeFavorites;
import com.ruoyi.recipes.service.IRecipesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 菜谱主表Controller
 * 
 * @author syy
 * @date 2025-08-13
 */
@RestController
@RequestMapping("/recipes/recipes")
public class RecipesController extends BaseController
{
    @Autowired
    private IRecipesService recipesService;

    /**
     * 查询菜谱主表列表
     */
    @PreAuthorize("@ss.hasPermi('recipes:recipes:list')")
    @GetMapping("/list")
    public TableDataInfo list(Recipes recipes)
    {
        startPage();
        List<Recipes> list = recipesService.selectRecipesList(recipes);
        return getDataTable(list);
    }

    /**
     * 导出菜谱主表列表
     */
    @PreAuthorize("@ss.hasPermi('recipes:recipes:export')")
    @Log(title = "菜谱主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Recipes recipes)
    {
        List<Recipes> list = recipesService.selectRecipesList(recipes);
        ExcelUtil<Recipes> util = new ExcelUtil<Recipes>(Recipes.class);
        util.exportExcel(response, list, "菜谱主表数据");
    }

    /**
     * 获取菜谱主表详细信息
     */
    @PreAuthorize("@ss.hasPermi('recipes:recipes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(recipesService.selectRecipesById(id));
    }

    /**
     * 新增菜谱主表
     */
    @PreAuthorize("@ss.hasPermi('recipes:recipes:add')")
    @Log(title = "菜谱主表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Recipes recipes)
    {
        return toAjax(recipesService.insertRecipes(recipes));
    }

    /**
     * 修改菜谱主表
     */
    @PreAuthorize("@ss.hasPermi('recipes:recipes:edit')")
    @Log(title = "菜谱主表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Recipes recipes)
    {
        return toAjax(recipesService.updateRecipes(recipes));
    }

    /**
     * 删除菜谱主表
     */
    @PreAuthorize("@ss.hasPermi('recipes:recipes:remove')")
    @Log(title = "菜谱主表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(recipesService.deleteRecipesByIds(ids));
    }
    
    /**
     * 添加菜谱评论
     */
    @PostMapping("/comment")
    public AjaxResult addComment(@RequestBody Comments comments)
    {
        return toAjax(recipesService.addComment(comments));
    }
    
    /**
     * 获取菜谱评论列表
     */
    @GetMapping("/comment/{recipeId}")
    public AjaxResult getComments(@PathVariable("recipeId") Long recipeId)
    {
        return success(recipesService.getCommentsByRecipeId(recipeId));
    }
    
    /**
     * 获取菜谱评论数量
     */
    @GetMapping("/comment/count/{recipeId}")
    public AjaxResult getCommentCount(@PathVariable("recipeId") Long recipeId)
    {
        return success(recipesService.getCommentCountByRecipeId(recipeId));
    }
    
    /**
     * 评论点赞/取消点赞
     */
    @PostMapping("/comment/like")
    public AjaxResult toggleCommentLike(@RequestBody Comments comments)
    {
        boolean isLiked = recipesService.toggleCommentLike(comments.getId(), comments.getUserId());
        return success(isLiked);
    }
    
    // ========== 收藏相关API ==========
    
    /**
     * 收藏/取消收藏菜谱
     */
    @PostMapping("/favorite")
    public AjaxResult toggleFavorite(@RequestBody RecipeFavorites recipeFavorites)
    {
        boolean isFavorited = recipesService.toggleFavorite(recipeFavorites.getRecipeId(), recipeFavorites.getUserId());
        return success(isFavorited);
    }
    
    /**
     * 获取菜谱收藏数量
     */
    @GetMapping("/favorite/count/{recipeId}")
    public AjaxResult getFavoriteCount(@PathVariable("recipeId") Long recipeId)
    {
        return success(recipesService.getFavoriteCountByRecipeId(recipeId));
    }
    
    /**
     * 获取用户收藏列表
     */
    @GetMapping("/favorite/user/{userId}")
    public AjaxResult getUserFavorites(@PathVariable("userId") Long userId)
    {
        return success(recipesService.getFavoritesByUserId(userId));
    }
    
    /**
     * 检查用户是否已收藏菜谱
     */
    @GetMapping("/favorite/check/{recipeId}/{userId}")
    public AjaxResult checkUserFavorite(@PathVariable("recipeId") Long recipeId, @PathVariable("userId") Long userId)
    {
        return success(recipesService.checkUserFavorite(recipeId, userId));
    }
    // ========== 推荐和标签相关API ==========

    /**
     * 获取推荐菜谱（按收藏量排序，排除指定用户）
     */
    @GetMapping("/recommend")
    public TableDataInfo getRecommendRecipes(Recipes recipes)
    {
        startPage();
        List<Recipes> list = recipesService.selectRecipesList(recipes);
        return getDataTable(list);
    }

    /**
     * 根据场景标签获取菜谱列表
     */
    @GetMapping("/by-scene-tag/{tagId}")
    public TableDataInfo getRecipesBySceneTag(@PathVariable("tagId") Long tagId, Recipes recipes)
    {
        startPage();
        recipes.setSceneTagId(tagId);
        List<Recipes> list = recipesService.selectRecipesBySceneTag(recipes);
        return getDataTable(list);
    }
}
